/*
 * Created: Author: Aléx Carvalho / Date: May 16, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-11 01:31:49 -0300 (Thu, 11 Jun 2015) $
 */
package com.cpm.view.zk.parts;

import com.cpm.Callback;
import com.cpm.view.Element;
import java.util.Optional;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.HtmlBasedComponent;

public class Window implements Element<HtmlBasedComponent> {

    private Optional<Callback> callbackClose = Optional.empty();
    private final org.zkoss.zul.Window window = new org.zkoss.zul.Window();

    public Window() {
    }

    @Override
    public final HtmlBasedComponent getElement() {
        return window;
    }

    protected final void appendChild(HtmlBasedComponent component) {
        window.appendChild(component);
    }

    public final void openModal() {
        window.setPage(Executions.getCurrent().getDesktop().getFirstPage());
        window.setClosable(true);
        window.setTitle("(window name)");
        window.doModal();
    }

    protected final void detach() {
        window.detach();

        callbackClose.ifPresent((Callback c) -> c.accept(null));
    }

    public final void setCallbackClose(Callback callback) {
        callbackClose = Optional.of(callback);
    }

}
